package mobi.weq.servlets.user;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mobi.weq.dao.UserDao;
import mobi.weq.model.User;

/**
 * Servlet implementation class RegisterUser
 */
@WebServlet("/user/register")
public class RegisterUser extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String displayName = request.getParameter("display_name");
		String password = request.getParameter("password");
		String email = request.getParameter("email");

		User us = new User();
		us.setUserName(username);
		us.setEmail(email);
		us.setPassword(password);
		us.setDisplayName(displayName);

		UserDao ud = new UserDao();
		try {
			ud.addUser(us);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		HttpSession session = request.getSession(true);
		session.setAttribute("user", us);
		response.sendRedirect("../home");
	}
}
